<?php
/**
 * Created by PhpStorm.
 * Author: sitewang@aliyun.com
 * CreateTime: 2023/9/13 01:10
 * Blog：www.myblogs.xyz
 */

namespace tests;

define('NEW_TIME', time());

// ### 需要手动初始化
require_once __DIR__ . '/../vendor/autoload.php';

((new \think\App())->http)->run();

use app\Request;
use think\App;

trait CreatesApplication
{
    function visitor($controller, $action, $data, $header = [], $method = 'POST')
    {
        $request = new Request();
        // $request->setUrl('/interfaces/order/create');
        $request->withPost($data);
        $request->withHeader(array_merge(
            ['HTTP_ACCEPT'=>'application/json'], $header
        ));
        $request->setMethod('POST');

        $app = (new App());
        $http = $app->http;
        $response = $http->run($request);
        // 重置请求对象
        $app->request->setAction($action);
        $app->http->end($response);

        $test = new $controller($app);
        $result = $test->$action();
        $content = $result->getContent();

        return json_decode($content);
    }
}